<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    .swiper-layout {
      width: 500px;
      height: 250px;
      border: 1px solid #ccc;
      margin: 100px auto;
      overflow: hidden;
      position: relative;
    }

    .swiper-container {
      height: 100%;
      transition: all 0.3s linear;
    }

    .swiper-container .swiper-item {
      width: 500px;
      height: 100%;
      font-size: 40px;
      color: #fff;
      line-height: 250px;
      text-align: center;
      float: left;
    }

    .swiper-container .swiper-item {
      background-color: orange;
    }

    .swiper-layout .left {
      width: 50px;
      height: 50px;
      background: #fff;
      position: absolute;
      top: 50%;
      transform: translate3d(20px, -50%, 0);
      cursor: pointer;
    }

    .swiper-layout .right {
      width: 50px;
      height: 50px;
      background: #fff;
      position: absolute;
      right: 0;
      top: 50%;
      transform: translate3d(-20px, -50%, 0);
      cursor: pointer;
    }

  </style>
</head>

<body>
  <div class="swiper-layout">
    <div class="swiper-container">
      <div class="swiper-item">One</div>
      <div class="swiper-item">Two</div>
      <div class="swiper-item">Three</div>
      <div class="swiper-item">Four</div>
    </div>
    <div class="left">Left</div>
    <div class="right">Right</div>
  </div>
  <script>
    const layOut = document.getElementsByClassName('swiper-layout')[0]
    const swiperContainer = document.getElementsByClassName('swiper-container')[0]
    const swiperItem = document.getElementsByClassName('swiper-item')
    const leftBtn = document.getElementsByClassName('left')[0]
    const rightBtn = document.getElementsByClassName('right')[0]
    const cloneNode = swiperItem[0].cloneNode(true)
    swiperContainer.appendChild(cloneNode)
    swiperContainer.style.width = layOut.clientWidth * swiperItem.length + 'px'
    let index = 0
    rightBtn.addEventListener('click', function () {
      swiperContainer.style.transition = 'all 0.3s linear'
      index++
      const offsetWidth = index * layOut.clientWidth
      swiperContainer.style.transform = `translate3d(-${offsetWidth}px,0,0)`
      if (index === swiperItem.length - 1) {
        setTimeout(() => {
          index = 0
          swiperContainer.style.transition = 'none'
          const offsetWidth = index * layOut.clientWidth
          swiperContainer.style.transform = `translate3d(-${offsetWidth}px,0,0)`
        }, 300)
      }
    })

    leftBtn.addEventListener('click', function () {
      swiperContainer.style.transition = 'all 0.3s linear'
      index--
      if (index <= 0) {
        index = swiperItem.length - 1
        swiperContainer.style.transition = 'none'
        setTimeout(() => {
          index--
          swiperContainer.style.transition = 'all 0.3s linear'
          const offsetWidth = index * layOut.clientWidth
          swiperContainer.style.transform = `translate3d(-${offsetWidth}px,0,0)`
        }, 0);
      }
      const offsetWidth = index * layOut.clientWidth
      swiperContainer.style.transform = `translate3d(-${offsetWidth}px,0,0)`
    })
  </script>
  <script src="./包装类.js"></script>
</body>

</html>
